﻿using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using NExplorer.AddIns.Samples;

namespace NExplorer.AddIns.Samples.Commands
{
	/// <summary>
	/// Demonstrates a command that works with many types, but when only one file is selected.
	/// (See <see cref="SampleMenuBuilder"/>.)
	/// </summary>
	internal class CopyContentsToClipboardCommand : CommandBase
	{
		internal static readonly List<LowerCaseString> Extensions = new List<LowerCaseString>();

		static CopyContentsToClipboardCommand()
		{
			Extensions.Add(new LowerCaseString("txt"));	
			Extensions.Add(new LowerCaseString("cs"));	
			Extensions.Add(new LowerCaseString("vb"));	
			Extensions.Add(new LowerCaseString("htm"));	
			Extensions.Add(new LowerCaseString("html"));	
		}

		public CopyContentsToClipboardCommand() 
			: base("Copy Contents to Clipboard")
		{}

		//internal static bool 
		protected override void Execute(FileSystemInfo fileSystemInfo)
		{
			FileInfo fileInfo = fileSystemInfo as FileInfo;

			if (fileInfo != null)
			{
				if (fileInfo.Length > 1024 * 1000)
				{
					string text = "The file is quite large. Are you sure you want to copy its contents to the clipboard?";
					
					if (MessageBox.Show(text, "Large file", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
					{
						CopyFileToClipboard(fileSystemInfo);
					}
				}
				else
				{
					CopyFileToClipboard(fileSystemInfo);
				}
			}
		}

		private void CopyFileToClipboard(FileSystemInfo info)
		{
			string text = File.ReadAllText(info.FullName);

			if (text.Length == 0)
			{
				MessageBox.Show("The file is empty and won't be copied.", "Empty File", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			else
			{
				Clipboard.SetText(text);
			}
		}
	}
}